package org.jeecg.modules.online.cgform.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.online.cgform.common.CommonEntity;
import org.jeecg.modules.online.cgform.entity.OnlCgformButton;
import org.jeecg.modules.online.cgform.entity.OnlCgformEnhanceJs;
import org.jeecg.modules.online.cgform.entity.OnlCgformField;
import org.jeecg.modules.online.cgform.entity.OnlCgformHead;
import org.jeecg.modules.online.cgform.model.HrefSlots;
import org.jeecg.modules.online.cgform.model.OnlColumn;
import org.jeecg.modules.online.cgform.model.OnlineConfigModel;
import org.jeecg.modules.online.cgform.model.OnlForeignKey;
import org.jeecg.modules.online.cgform.model.ScopedSlots;
import org.jeecg.modules.online.cgform.model.FieldModel;
import org.jeecg.modules.online.cgform.service.IOnlCgformFieldService;
import org.jeecg.modules.online.cgform.service.IOnlCgformHeadService;
import org.jeecg.modules.online.cgform.service.IOnlineService;
import org.jeecg.modules.online.cgform.util.EnhanceJsUtil;
import org.jeecg.modules.online.cgform.util.FieldSlotType;
import org.jeecg.modules.online.cgform.util.SqlSymbolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("onlineService")
public class OnlineServiceImpl implements IOnlineService {
    @Autowired
    private IOnlCgformFieldService onlCgformFieldService;
    @Autowired
    private IOnlCgformHeadService onlCgformHeadService;
    @Autowired
    private ISysBaseAPI sysBaseAPI;

    public OnlineConfigModel queryOnlineConfig(OnlCgformHead head) {
        String id = head.getId();
        List<OnlCgformField> onlCgformFields = this.getFields(id);
        List<String> var4 = this.onlCgformFieldService.selectOnlineHideColumns(head.getTableName());
        List<OnlColumn> var5 = new ArrayList<>();
        Map<String,List<DictModel>> var6 = new HashMap<>();
        List<HrefSlots> var7 = new ArrayList<>();
        List<OnlForeignKey> var8 = new ArrayList<>();
        List<String> var9 = new ArrayList<>();
        Iterator<OnlCgformField> iterator = onlCgformFields.iterator();

        String var14;
        while(iterator.hasNext()) {
            OnlCgformField onlCgformField = (OnlCgformField)iterator.next();
            String var12 = onlCgformField.getDbFieldName();
            String var13 = onlCgformField.getMainTable();
            var14 = onlCgformField.getMainField();
            if (oConvertUtils.isNotEmpty(var14) && oConvertUtils.isNotEmpty(var13)) {
                OnlForeignKey var15 = new OnlForeignKey(var12, var14);
                var8.add(var15);
            }

            if (1 == onlCgformField.getIsShowList() && !"id".equals(var12) && !var4.contains(var12) && !var9.contains(var12)) {
                OnlColumn onlColumn = new OnlColumn(onlCgformField.getDbFieldTxt(), var12);
                String var16 = onlCgformField.getDictField();
                String fieldShowType = onlCgformField.getFieldShowType();
                if (oConvertUtils.isNotEmpty(var16) && !"popup".equals(fieldShowType)) {
                	List<DictModel> var18 = new ArrayList<>();
                    if (oConvertUtils.isNotEmpty(onlCgformField.getDictTable())) {
                        var18 = this.sysBaseAPI.queryTableDictItemsByCode(onlCgformField.getDictTable(), onlCgformField.getDictText(), var16);
                    } else if (oConvertUtils.isNotEmpty(onlCgformField.getDictField())) {
                        var18 = this.sysBaseAPI.queryDictItemsByCode(var16);
                    }

                    var6.put(var12, var18);
                    onlColumn.setCustomRender(var12);
                }

                List<DictModel> var29;
                if ("switch".equals(fieldShowType)) {
                    var29 = org.jeecg.modules.online.cgform.util.SqlSymbolUtil.a(onlCgformField);
                    var6.put(var12, var29);
                    onlColumn.setCustomRender(var12);
                }

                List<DictModel> var20;
                String var31;
                if ("link_down".equals(fieldShowType)) {
                    var31 = onlCgformField.getDictTable();
                    CommonEntity var19 = (CommonEntity)JSONObject.parseObject(var31, CommonEntity.class);
                    var20 = this.sysBaseAPI.queryTableDictItemsByCode(var19.getTable(), var19.getTxt(), var19.getKey());
                    var6.put(var12, var20);
                    onlColumn.setCustomRender(var12);
                    var5.add(onlColumn);
                    String var21 = var19.getLinkField();
                    this.a(onlCgformFields, var9, var5, var12, var21);
                }

                if ("sel_tree".equals(fieldShowType)) {
                    String[] var33 = onlCgformField.getDictText().split(",");
                    List<DictModel> var30 = this.sysBaseAPI.queryTableDictItemsByCode(onlCgformField.getDictTable(), var33[2], var33[0]);
                    var6.put(var12, var30);
                    onlColumn.setCustomRender(var12);
                }

                if ("cat_tree".equals(fieldShowType)) {
                    var31 = onlCgformField.getDictText();
                    if (oConvertUtils.isEmpty(var31)) {
                        String var32 = org.jeecg.modules.online.cgform.util.SqlSymbolUtil.e(onlCgformField.getDictField());
                        var20 = this.sysBaseAPI.queryFilterTableDictInfo("SYS_CATEGORY", "NAME", "ID", var32);
                        var6.put(var12, var20);
                        onlColumn.setCustomRender(var12);
                    } else {
                        onlColumn.setCustomRender("_replace_text_" + var31);
                    }
                }

                if ("sel_depart".equals(fieldShowType)) {
                    var29 = this.sysBaseAPI.queryAllDepartBackDictModel();
                    var6.put(var12, var29);
                    onlColumn.setCustomRender(var12);
                }

                if ("sel_user".equals(onlCgformField.getFieldShowType())) {
                    var29 = this.sysBaseAPI.queryTableDictItemsByCode("SYS_USER", "REALNAME", "USERNAME");
                    var6.put(var12, var29);
                    onlColumn.setCustomRender(var12);
                }

                if (fieldShowType.indexOf(FieldSlotType.FILE) >= 0) {
                    onlColumn.setScopedSlots(new ScopedSlots("fileSlot"));
                } else if (fieldShowType.indexOf(FieldSlotType.IMAGE) >= 0) {
                    onlColumn.setScopedSlots(new ScopedSlots("imgSlot"));
                } else if (fieldShowType.indexOf(FieldSlotType.EDITOR) >= 0) {
                    onlColumn.setScopedSlots(new ScopedSlots("htmlSlot"));
                } else if (fieldShowType.equals(FieldSlotType.DATE)) {
                    onlColumn.setScopedSlots(new ScopedSlots("dateSlot"));
                } else if (fieldShowType.equals(FieldSlotType.PCA)) {
                    onlColumn.setScopedSlots(new ScopedSlots("pcaSlot"));
                } else {
                    onlColumn.setScopedSlots(new ScopedSlots("formatSlot"));
                }

                String jsEnhance = onlCgformField.getJsEnhance();
                if (!org.springframework.util.StringUtils.isEmpty(jsEnhance)) {
                    onlColumn.setJsEnhance(onlCgformField.getJsEnhance());
                }

                if (StringUtils.isNotBlank(onlCgformField.getFieldHref())) {
                    var31 = "fieldHref_" + var12;
                    onlColumn.setHrefSlotName(var31);
                    var7.add(new HrefSlots(var31, onlCgformField.getFieldHref()));
                }

                if ("1".equals(onlCgformField.getSortFlag())) {
                    onlColumn.setSorter(true);
                }

                if (!"link_down".equals(fieldShowType)) {
                    var5.add(onlColumn);
                }
            }
        }

        OnlineConfigModel var22 = new OnlineConfigModel();
        var22.setCode(id);
        var22.setTableType(head.getTableType());
        var22.setFormTemplate(head.getFormTemplate());
        var22.setDescription(head.getTableTxt());
        var22.setCurrentTableName(head.getTableName());
        var22.setPaginationFlag(head.getIsPage());
        var22.setCheckboxFlag(head.getIsCheckbox());
        var22.setScrollFlag(head.getScroll());
        var22.setColumns(var5);
        var22.setDictOptions(var6);
        var22.setFieldHrefSlots(var7);
        var22.setForeignKeys(var8);
        var22.setHideColumns(var4);
        List<OnlCgformButton> var23 = this.onlCgformHeadService.queryButtonList(id, true);
        List<OnlCgformButton> var24 = new ArrayList<>();
        Iterator<OnlCgformButton> var25 = var23.iterator();

        while(var25.hasNext()) {
            OnlCgformButton var27 = var25.next();
            if (!var4.contains(var27.getButtonCode())) {
                var24.add(var27);
            }
        }

        var22.setCgButtonList(var24);
        OnlCgformEnhanceJs var26 = this.onlCgformHeadService.queryEnhanceJs(id, "list");
        if (var26 != null && oConvertUtils.isNotEmpty(var26.getCgJs())) {
            var14 = EnhanceJsUtil.getJsFunction(var26.getCgJs(), var23);
            var22.setEnhanceJs(var14);
        }

        if ("Y".equals(head.getIsTree())) {
            var22.setPidField(head.getTreeParentIdField());
            var22.setHasChildrenField(head.getTreeIdField());
            var22.setTextField(head.getTreeFieldname());
        }

        return var22;
    }

    private void a(List<OnlCgformField> var1, List<String> var2, List<OnlColumn> var3, String var4, String var5) {
        if (oConvertUtils.isNotEmpty(var5)) {
            String[] var6 = var5.split(",");
            String[] var7 = var6;
            int var8 = var6.length;

            for(int var9 = 0; var9 < var8; ++var9) {
                String var10 = var7[var9];
                Iterator<OnlCgformField> var11 = var1.iterator();

                while(var11.hasNext()) {
                    OnlCgformField var12 = var11.next();
                    String var13 = var12.getDbFieldName();
                    if (1 == var12.getIsShowList() && var10.equals(var13)) {
                        var2.add(var10);
                        OnlColumn var14 = new OnlColumn(var12.getDbFieldTxt(), var13);
                        var14.setCustomRender(var4);
                        var3.add(var14);
                        break;
                    }
                }
            }
        }

    }

    public JSONObject queryOnlineFormObj(OnlCgformHead head, OnlCgformEnhanceJs onlCgformEnhanceJs) {
        JSONObject var3 = new JSONObject();
        String var4 = head.getId();
        List<OnlCgformField> var5 = this.onlCgformFieldService.queryAvailableFields(head.getId(), head.getTableName(), (String)null, false);
        List<String> var6 = this.onlCgformFieldService.queryDisabledFields(head.getTableName());
        EnhanceJsUtil.a(onlCgformEnhanceJs, head.getTableName(), var5);
        FieldModel var7 = null;
        if ("Y".equals(head.getIsTree())) {
            var7 = new FieldModel();
            var7.setCodeField("id");
            var7.setFieldName(head.getTreeParentIdField());
            var7.setPidField(head.getTreeParentIdField());
            var7.setPidValue("0");
            var7.setHsaChildField(head.getTreeIdField());
            var7.setTableName(SqlSymbolUtil.getSubstring(head.getTableName()));
            var7.setTextField(head.getTreeFieldname());
        }

        JSONObject var8 = SqlSymbolUtil.getFiledJson(var5, var6, var7);
        var8.put("table", head.getTableName());
        var3.put("schema", var8);
        var3.put("head", head);
        List<OnlCgformButton> var9 = this.onlCgformHeadService.queryButtonList(var4, false);
        if (var9 != null && var9.size() > 0) {
            var3.put("cgButtonList", var9);
        }

        if (onlCgformEnhanceJs != null && oConvertUtils.isNotEmpty(onlCgformEnhanceJs.getCgJs())) {
            String var10 = EnhanceJsUtil.c(onlCgformEnhanceJs.getCgJs(), var9);
            onlCgformEnhanceJs.setCgJs(var10);
            var3.put("enhanceJs", EnhanceJsUtil.getCgJs(onlCgformEnhanceJs.getCgJs()));
        }

        return var3;
    }

    public JSONObject queryOnlineFormObj(OnlCgformHead head) {
        OnlCgformEnhanceJs var2 = this.onlCgformHeadService.queryEnhanceJs(head.getId(), "form");
        return this.queryOnlineFormObj(head, var2);
    }

    private List<OnlCgformField> getFields(String cgformHeadId) {
        LambdaQueryWrapper<OnlCgformField> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OnlCgformField::getCgformHeadId, cgformHeadId);
        lambdaQueryWrapper.orderByAsc(OnlCgformField::getOrderNum);
        return this.onlCgformFieldService.list(lambdaQueryWrapper);
    }
}
